import * as types from './types'
import Req from '../util/request'

function setCourses( {courses} ){
	return {
		type: types.SET_COURSES,
		courses
	}
}

export function fetchCourse( key ){
	return (dispatch, getState) => {
		const {offset} = getState().paginator;
		key = key || getState().searchKey
	    return Req.get(`/api/courses?q=${key}&offset=${offset}`)
		    .catch( (ex) => {
		      console.log(ex);
			});
  	}
}

export function setCourse(courses) {
	return (dispatch, getState) => {
		dispatch(setCourses({courses}));
  	}
}

export function setPagination(res) {
	return (dispatch, getState) => {
		dispatch({
            type: types.SET_PAGINATION,
            res
        });
  	}
}

export function clearCourse() {
	return (dispatch, getState) => {
		dispatch({
			type: types.CLEAR_COURSES
		})
	}
}

export function setRecomm(course){
	return (dispatch, getState) => {
		return Req.post('/api/recomm', course)
	}
}


function setRecomms( {recomms} ){
	return {
		type: types.SET_RECOMMS,
		recomms
	}
}


export function fetchRemmons( key ){
	return (dispatch, getState) => {
	    return Req.get(`/api/recomms?`).then(result => {
	    	console.log(result)
	    	console.log('resutl')
	      dispatch(setRecomms({recomms: result.data}));
	    }).catch( (ex) => {
	      console.log(ex);
		});
  }
}

export function addBookmark(course) {
	return async (dispatch, getState) => {
		await Req.put(`/api/course/bookmark`, {course})
	}
}
export function addShare(course) {
	return async (dispatch, getState) => {
		await Req.put(`/api/course/share`, {course})
	}
}
export function addHit(course) {
	return async (dispatch, getState) => {
		await Req.put(`/api/course/hit`, {course})
	}
}

